package com.ys.controller.web.finance;

import com.ys.config.MenuId;
import com.ys.dto.SubGridConfigDto;
import com.ys.entity.finance.TcwFinanceSalaryClassify;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.Flow;
import com.ys.vo.W;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 财务工资小计部门维护表
 */
@Controller
@RequestMapping("/web/finance/tcwFinanceSalaryClassify")
public class WebTcwFinanceSalaryClassifyController {
	private static final Logger logger = LoggerFactory.getLogger(WebTcwFinanceSalaryClassifyController.class);

	@Autowired
	private CommonApiService<TcwFinanceSalaryClassify> commonApiService;

    /*
     * 新增
     */
    @GetMapping("/toAdd")
    public String toAdd(Model model, HttpServletRequest request, HttpServletResponse response) {
        try {
            Map<String, Object> extraParam = new HashMap<>();
            //关联数据
            extraParam.put("status", 1);
            model.addAttribute("onProcess", 0);
            // 子列表配置信息
            SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.TCW_FinanceSalaryClassify, new ArrayList<Map<String, Object>>());
            return  commonApiService.toAdd(model, new TcwFinanceSalaryClassify(), MenuId.TCW_FinanceSalaryClassify, extraParam, subGridConfig);
        }catch (Exception e) {
            logger.error("新增页面出错：",e.getMessage(),e);
            return W.error(model, e.getMessage());
        }
    }

    /*
     * 编辑
     */
    @GetMapping("/toEdit")
    public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response, Long id) {
        try {
            model.addAttribute("onProcess", 0);
            // 子列表配置信息
            SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.TCW_FinanceSalaryClassify,getItemList(id));
            return commonApiService.toEdit(model,id, new TcwFinanceSalaryClassify(), MenuId.TCW_FinanceSalaryClassify, null, subGridConfig);
        }catch (Exception e) {
            logger.error("编辑页面出错：",e.getMessage(),e);
            return W.error(model, e.getMessage());
        }
    }


    /*
     * 详情
     */
    @GetMapping("/toDetail")
    public String toDetail(Model model, HttpServletRequest request, HttpServletResponse response, Long id) {
        try {
            model.addAttribute("onProcess", 1);
            // 子列表配置信息
            SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.TCW_FinanceSalaryClassify,getItemList(id));
            //子表按钮
            subGridConfig.setButtonArr(new ArrayList<>());
            return commonApiService.toDetail(model,id, new TcwFinanceSalaryClassify(), MenuId.TCW_FinanceSalaryClassify, null, subGridConfig);
        }catch (Exception e) {
            logger.error("详情页面出错：",e.getMessage(),e);
            return W.error(model, e.getMessage());
        }
    }


    //获取子列表数据
    public List<Map<String, Object>> getItemList(Long id){
        TcwFinanceSalaryClassify entity = new TcwFinanceSalaryClassify().setId(id).queryFirst();
        List<Map<String, Object>> itemList = new TcwFinanceSalaryClassify()
        .addLeftJoin("TS_OrganizationInfo O ON O.Orgid = A.Orgid ")
                //.addField("O.OrgName AS orgName")
                .addField("CASE " +
                        "WHEN O.Orgid = 'D250' THEN '研发部' " +
                        "WHEN O.Orgid = 'C101' THEN '总裁办' " +
                        " ELSE O.OrgName END orgName")//部门名称
                .addField("CASE " +
                        "WHEN O.Orgid = 'D250' THEN '研发部' " +
                        "WHEN O.Orgid = 'C101' THEN '总裁办' " +
                        " ELSE O.OrgName END orgNameNew")//部门名称
                .setClassifyName(entity.getClassifyName())
                .queryMap();
        return itemList;
    }



}






